﻿package com.view 
{
	import com.controller.OverStartupCommand;
	import com.model.PicListProxy;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * ...
	 * @author Mr.zheng
	 */
	public class NumMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "numMediator_name";
		public static const CHANGEPIC:String = "numMediator_ChangePic";
		
		private var numX:int=300;
		private var numY:int=335;
		private var space:int=40;
		
		
		public function NumMediator(mediatorName:String=null,viewComponent:Object=null) 
		{
			super(mediatorName, viewComponent);
		}
		override public function onRegister():void 
		{
			super.onRegister();
		}
		override public function listNotificationInterests():Array 
		{
			return [OverStartupCommand.DATACOMPLETE,CHANGEPIC];
		}
		override public function handleNotification(notification:INotification):void 
		{
			super.handleNotification(notification);
			var name:String = notification.getName();
			var obj:Object = notification.getBody();
			switch(name) {
				case OverStartupCommand.DATACOMPLETE:
				//数据ok了~，开始布局
					var picList:Array = facade.retrieveProxy(PicListProxy.NAME).getData().list;
					layout(picList);
					break;
				case CHANGEPIC:
				//收到改变图片消息
					break;
			}
		}
		/**
		 * 根据arr来显示数字按钮，并且布局
		 * @param	arr
		 */
		private function layout(arr:Array):void {
			var app:WheelPic=facade.retrieveMediator(WheelPicMediator.NAME).getViewComponent() as WheelPic;
			for(var i:* in arr){
				var numMc:NumMc=new NumMc();
				numMc.btn.label=i+1;
				numMc.x=i*space+numX;
				numMc.y=numY;
				app.addElement(numMc);
				numMc.addEventListener(MouseEvent.CLICK,onClick);
			}
		}
		private function onClick(e:Event):void{
			var num:int=int(NumMc(e.currentTarget).btn.label)-1;
			sendNotification(CHANGEPIC,num);
		}
		
	}

}